package com.example.Sao.MyActivity;

import com.example.Sao.R;
import com.example.Sao.Serivce.SerialPortService;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import io.objectbox.annotation.Transient;

public class SettingsActivity extends MyBaseActivity {
	private static final String TAG = "SettingsActivity"; 
	
	public SerConnection  mSerConnection;
	SerialPortService.MyBinder  mBinder = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		SettingFragment settingFragment = new SettingFragment();
	
		
		transaction.add(R.id.container,settingFragment);
		transaction.commitAllowingStateLoss();
		
		Log.d(TAG, "onCreate");
		
	    Intent mIntent = new Intent();
	    ComponentName componentName = new ComponentName("com.android.barcodescanner","com.android.barcodeservice.SerialPortService");
	    mIntent.setComponent(componentName);
	    mSerConnection = new SerConnection();
		bindService(mIntent, mSerConnection, Context.BIND_AUTO_CREATE);
		
	}

	private class SettingFragment extends PreferenceFragment{

		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.preference_settings);
			//getPreferenceManager().setSharedPreferencesName("config");
			addPreferenceHead(getPreferenceScreen());
		}
		
	}
	
	
	public void addPreferenceHead(PreferenceScreen mPreferenceScreen){
		
		PreferenceHead ph = new PreferenceHead(this);
		ph.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				int radioButtonId = group.getCheckedRadioButtonId();
				
		  		SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
		  		SharedPreferences.Editor editor = mSP.edit();
		  		
				if( radioButtonId == R.id.radioSig){
					Log.e(TAG, "radioSig");
					if(mBinder != null){
						mBinder.openUartSig();
					}

		  	  		editor.putBoolean(MyApplication.SCAN_MODE_SIG,  true);

				}else if( radioButtonId == R.id.radioQR){
					Log.e(TAG, "radioQR");
					
					if(mBinder != null){
						mBinder.openUartQR();
					}
					
		  	  		editor.putBoolean(MyApplication.SCAN_MODE_SIG,  false);
		  	  		
				}
				
		  		editor.commit();
			}
		});
		
		ph.setOnCheckedChangeScankey(new android.widget.CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				int ButtonId = buttonView.getId();
				SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
		  		SharedPreferences.Editor editor = mSP.edit();
		  		if(ButtonId == R.id.radioLeft){
 					Log.e(TAG, "radioLeft");
					if(isChecked){
						editor.putBoolean(MyApplication.SCAN_MODE_LEFT,  true);
					}else{
						editor.putBoolean(MyApplication.SCAN_MODE_LEFT,  false);
					}
 					
				}else if(ButtonId == R.id.radioRight){
 					 Log.e(TAG, "radioRight");
					 if(isChecked){
						 editor.putBoolean(MyApplication.SCAN_MODE_RIGHT,  true);
					 }else{
						 editor.putBoolean(MyApplication.SCAN_MODE_RIGHT,  false);
					 }
				}
		  		editor.commit();
			}
			
		});
		ph.setOrder(-1);
		mPreferenceScreen.addPreference(ph);
   }

	  @Override
	protected void onDestroy() {
		super.onDestroy();
		
		unbindService(mSerConnection);
	}	
  
  public class SerConnection implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			  mBinder = (SerialPortService.MyBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			   mBinder = null;
		}
	}
	
}
